<?
	function email_invoice($id)
	{
		$websitename_eng="18eighty.com";
		$get_bill=mysql_query("select * from os_bill_details where id=$id");
		$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
		$get_item=mysql_query("select * from os_bill_items where bid=$id")or die("line 11".mysql_error());
		$count=mysql_num_rows($get_bill);
		$get_info=mysql_query("select * from os_contactinfo where id=1");
		$i=mysql_fetch_array($get_info,MYSQL_ASSOC);
				
		$to=$b['email'];
		$subject=$websitename_eng." Purchase Invoice";
		
		$body="<html>
		<head>
		<style type='text/css'>
		BODY {
				BACKGROUND: #fff; MARGIN: 0px; FONT: 12px/140% 'sans-serif'; TEXT-ALIGN: center
			}
			.text{font:10px}
			.solidborder{font:12px;border:1px solid #000000}
		</style>
		</head>
		
		<body >
	<table width='600' bgcolor='#ffffff' align='center' style='border:1px solid #000000'>
	<tr valign='top'>
	<td align='left'><font style='font-size:16px;color:#666666'><strong>18Eighty.com</strong></font><br>
	Tel: ".$i['contact_number']."<br>
	Fax: ".$i['fax']."<br>
	
	</td>
	<td align='right'><img src='".$i['logo_url']."/images/logo.jpg'></td>
	</tr>
	<tr>
		<td colspan='2' bgcolor='#666699' align='center' height='20'><font  style='color:#ffffff;font-size:14px;font-weight:bold'>INVOICE</font></td>
	</tr>
	<tr>
		<td colspan='2' align='right'>
			<table align='right' width='60%' class='text'> 
				<tr align='left'>
					<td width='65%'>Invoice number</td><td width='1%'>:</td><td width='34%'>".$b['invoice_number']."	</td>
				</tr>
				<tr  align='left'>
					<td width='65%'>Invoice date</td><td width='1%'>:</td><td width='34%'>".$b['create_date']."</td>
				</tr>
				<tr>
					<td align='left'>To:</td>
				</tr>";
				
				
				if($b['state']!=0)
				{
					$get_place=mysql_query('select * from os_state where id='.$b['state'])or die(mysql_error());
					$place=mysql_result($get_place,0,'name_eng');
					$state=$place;
				}
				else
				{
					$state='-';
				}
				
				if($b['country']!=0)
				{
				
					$get_country=mysql_query('select * from os_country where id='.$b['country'])or die(mysql_error());
					$country=mysql_result($get_country,0,'engname');
					$word_country=$country;
				}
				else
				{
					$word_country='Other';
				} 
						
				$body.="
				<tr>
					<td colspan='3' align='left'>
					".$b['receiver_name']."
					<br>
					".$b['email']."
					<br>
					".$b['address1']."
					<br>
					".$b['address2']."
					<br>
					".$b['postcode']."
					<br>
					".$state."
					<br>
					".$word_country."
					</td>
				</tr>
				<tr>
					<td></td>
				</tr>
				
		  </table>
		
		</td>
	</tr>
	<tr>
		<td colspan='2'>
			<table width='90%' bgcolor='#CCCCCC' cellpadding='1' cellspacing='1' class='text' align=center>
				<tr bgcolor='#F3F3F3' align='center'>
					<td width='39%'>Product</td><td width='19%'>Unit price(RM)</td><td width='14%'>Product code</td><td width='12%'>Quantity</td><td width='16%'>Total(RM)</td>
				</tr>";
				
			
			$total_item=0;
			$total_piece=0;
			$total_allprice=0;
			while($c=mysql_fetch_array($get_item))
			{
				$total_price=0;
				$get_product=mysql_query("select a.product_name_eng,a.product_code,b.description from os_products a inner join os_product_inventory b on a.id=b.pid where b.id=".$c['item_id']);
				$a=mysql_fetch_array($get_product,MYSQL_ASSOC);
				$total_price=$c['price']*$c['quantity'];
				$total_allprice=$total_allprice+$total_price;
						
																
			
				$body.="<tr bgcolor='#FFFFFF' align='center'>
					<td>".$a['product_name_eng']."</td><td>".$c['price']."</td><td>".$a['product_code']."</td><td>".$c['quantity']."</td>
					<td align='right'>".number_format($total_price,2)."</td>
				</tr>";
							
					} 
					$total_amount=$b['transport_fees']+$b['total_amount']; 
					$discount=($b['discount']/100)*$total_amount;
					$final_amount=$total_amount-$discount;
			
			$body.="
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' >Shipment Fee</td><td align='right'>".number_format($b['transport_fees'],2)."</td>
			</tr>
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' bgcolor='#FFFFFF'>Discount</td><td align='right'>- ".$b['discount']."%</td>
			</tr>
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' bgcolor='#FFFFFF'>Grand Total</td><td align='right'>".number_format($final_amount,2)."</td>
			</tr>
			</table>
		
		</td>
	</tr>
	<tr>
		<td align='left'>";
		
				$dayleft=7-dayDiff($b['verified_date']);
				$timeStamp = strtotime($b['verified_date']);
				$timeStamp += 24 * 60 * 60 * $dayleft; 
				$expiryDate = date('Y-m-d', $timeStamp);
			$get_bank=mysql_query("select * from os_bankacc where id=1");
	
			$bank=mysql_fetch_array($get_bank,MYSQL_ASSOC);
			
			$body.="<br><br>Please make payment within <font color=red>".$dayleft."</font> day(s),<br>
			the expiry date for this order is on <font color=red>".$expiryDate."</font>
			<br>
			<br>
			Kindly make payment to the following:
			<table cellpadding='1' cellspacing='1' align='left' class='solidborder'>
				<tr align='left'><td>Bank</td><td>:</td><td>".$bank['bank_name']."</td></tr>
				<tr align='left'><td>Company Name</td><td>:</td><td>".$bank['acc_name']."</td></tr>
				<tr align='left'><td>Acc. Number</td><td>:</td><td>".$bank['acc_no']."</td></tr>
			</table>
			 
			
		</td>
	</tr>
	<tr>
		<td>Note: This email will serve as an official invoice for this payment.</td>
	</tr>
	
	</table>
	";
	
	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=gb2312\r\n";
	$headers .= "From: $websitename_eng Admin";		
	
	mail($to,$subject,$body,$headers);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	function email_receipt($id)
	{
		$websitename_eng="18eighty.com";
		$get_bill=mysql_query("select * from os_bill_details where id=$id");
		$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
		$get_item=mysql_query("select * from os_bill_items where bid=$id")or die("line 11".mysql_error());
		$count=mysql_num_rows($get_bill);
		$get_info=mysql_query("select * from os_contactinfo where id=1");
		$i=mysql_fetch_array($get_info,MYSQL_ASSOC);
				
		$to=$b['email'];
		$subject=$websitename_eng." Payment Receipt";
		
		$body="<html>
		<head>
		<style type='text/css'>
		BODY {
				BACKGROUND: #fff; MARGIN: 0px; FONT: 12px/140% 'sans-serif'; TEXT-ALIGN: center
			}
			.text{font:10px}
			.solidborder{font:12px;border:1px solid #000000}
		</style>
		</head>
		
		<body >
	<table width='600' bgcolor='#ffffff' align='center' style='border:1px solid #000000'>
	<tr valign='top'>
	<td align='left'><font style='font-size:16px;color:#666666'><strong>18Eighty.com</strong></font><br>
	Tel: ".$i['contact_number']."<br>
	Fax: ".$i['fax']."<br>
	
	</td>
	<td align='right'><img src='".$i['logo_url']."/images/logo.jpg'></td>
	</tr>
	<tr>
		<td colspan='2' bgcolor='#666699' align='center' height='20'><font  style='color:#ffffff;font-size:14px;font-weight:bold'>Payment Receipt</font></td>
	</tr>
	<tr>
		<td colspan='2' align='right'>
			<table align='right' width='60%' class='text'> 
				<tr align='left'>
					<td width='65%'>Invoice number</td><td width='1%'>:</td><td width='34%'>".$b['invoice_number']."	</td>
				</tr>
				<tr  align='left'>
					<td width='65%'>Invoice date</td><td width='1%'>:</td><td width='34%'>".$b['create_date']."</td>
				</tr>
				<tr>
					<td align='left'>To:</td>
				</tr>";
				
				
				if($b['state']!=0)
				{
					$get_place=mysql_query('select * from os_state where id='.$b['state'])or die(mysql_error());
					$place=mysql_result($get_place,0,'name_eng');
					$state=$place;
				}
				else
				{
					$state='-';
				}
				
				if($b['country']!=0)
				{
				
					$get_country=mysql_query('select * from os_country where id='.$b['country'])or die(mysql_error());
					$country=mysql_result($get_country,0,'engname');
					$word_country=$country;
				}
				else
				{
					$word_country='Other';
				} 
						
				$body.="
				<tr>
					<td colspan='3' align='left'>
					".$b['receiver_name']."
					<br>
					".$b['email']."
					<br>
					".$b['address1']."
					<br>
					".$b['address2']."
					<br>
					".$b['postcode']."
					<br>
					".$state."
					<br>
					".$word_country."
					</td>
				</tr>
				<tr>
					<td></td>
				</tr>
				
		  </table>
		
		</td>
	</tr>
	<tr>
		<td colspan='2' align='left'>
			Dear ".$b['receiver_name'].",<br>
			Thank you for your payment.
			<br><br>
			Payment receipt for invoice ".$b['invoice_number'].".<br>
			
			
			<table width='90%' bgcolor='#CCCCCC' cellpadding='1' cellspacing='1' class='text' align=center>
				<tr bgcolor='#F3F3F3' align='center'>
					<td width='39%'>Product</td><td width='19%'>Unit price(RM)</td><td width='14%'>Product code</td><td width='12%'>Quantity</td><td width='16%'>Total(RM)</td>
				</tr>";
				
			
			$total_item=0;
			$total_piece=0;
			$total_allprice=0;
			while($c=mysql_fetch_array($get_item))
			{
				$total_price=0;
				$get_product=mysql_query("select a.product_name_eng,a.product_code,b.description from os_products a inner join os_product_inventory b on a.id=b.pid where b.id=".$c['item_id']);
				$a=mysql_fetch_array($get_product,MYSQL_ASSOC);
				$total_price=$c['price']*$c['quantity'];
				$total_allprice=$total_allprice+$total_price;
						
																
			
				$body.="<tr bgcolor='#FFFFFF' align='center'>
					<td>".$a['product_name_eng']."</td><td>".$c['price']."</td><td>".$a['product_code']."</td><td>".$c['quantity']."</td>
					<td align='right'>".number_format($total_price,2)."</td>
				</tr>";
							
					} 
					$total_amount=$b['transport_fees']+$b['total_amount']; 
					$discount=($b['discount']/100)*$total_amount;
					$final_amount=$total_amount-$discount;
			
			$body.="
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' >Shipment Fee</td><td align='right'>".number_format($b['transport_fees'],2)."</td>
			</tr>
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' bgcolor='#FFFFFF'>Discount</td><td align='right'>- ".$b['discount']."%</td>
			</tr>
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' bgcolor='#FFFFFF'>Grand Total</td><td align='right'>".number_format($final_amount,2)."</td>
			</tr>
			<tr bgcolor='#FFFFFF'>
				<td colspan='4' align='right' bgcolor='#FFFFFF'>Amount Paid</td><td align='right'>".number_format($final_amount,2)."</td>
			</tr>

			</table>
		
		</td>
	</tr>
	<tr>
		<td align='left'>";
		
				$dayleft=7-dayDiff($b['verified_date']);
				$timeStamp = strtotime($b['verified_date']);
				$timeStamp += 24 * 60 * 60 * $dayleft; 
				$expiryDate = date('Y-m-d', $timeStamp);
		
			$body.="
		<tr>
		<td align=left> Your order will be delivered to the address which you have provided. Kindly check on the status of your order in the next 48 hours.
		<br>
		Note: This email will serve as an official receipt for this payment.

		</td>
	</tr>
	
	</table>
	";
	
	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=gb2312\r\n";
	$headers .= "From: $websitename_eng Admin";		
	
	mail($to,$subject,$body,$headers);
	}

?>